'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log("传过来的");
	console.log('event : ', event)
	//初始化一个数据库
	const db = uniCloud.database();

	//增加之前先查一下，如果有就不增加了


	let timeDateInfo;
	let dbRes;

	if (event.typeName == "setData") {
		let dates = await db.collection("index-article").where({
			date: event.date
		}).get();

		if (dates.affectedDocs <= 0) { //值为1说明有一条符合，值为<=0说明不存在这个表段，初始化进去
			console.log("不存在");
			timeDateInfo = {
				date: event.date,
				cDay: event.cDay,
				ncWeek: event.ncWeek,
				IMonthCn: event.IMonthCn,
				IDayCn: event.IDayCn,
				textArticle: event.textArticle,
				love: event.love,
				comments: event.comments
			}
			//将数据传到云数据库中
			await db.collection("index-article").add(timeDateInfo)

			dbRes = await db.collection("index-article").get();

		} else {
			//查询相关信息
			console.log("存在");
			dbRes = await db.collection("index-article").get();
			console.log(dbRes);
		}
	}

	//返回数据给客户端
	return dbRes
};
